package com.yuzhi.master.drainMsgTemplate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuzhi.common.core.domain.entity.SysDictData;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;
import com.yuzhi.master.sewage.rmstSz.entity.vo.DrainRmstYwDayVo;
import com.yuzhi.system.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.yuzhi.master.drainMsgTemplate.domain.bo.DrainMsgTemplateBo;
import com.yuzhi.master.drainMsgTemplate.domain.vo.DrainMsgTemplateVo;
import com.yuzhi.master.drainMsgTemplate.domain.request.DrainMsgTemplateReq;
import com.yuzhi.master.drainMsgTemplate.domain.DrainMsgTemplate;
import com.yuzhi.master.drainMsgTemplate.mapper.DrainMsgTemplateMapper;
import com.yuzhi.master.drainMsgTemplate.service.IDrainMsgTemplateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 消息通知模板Service业务层处理
 *
 * @author coding123
 * @date 2025-08-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DrainMsgTemplateServiceImpl implements IDrainMsgTemplateService {

    private final DrainMsgTemplateMapper baseMapper;

    private final ISysDictTypeService iSysDictTypeService;

    /**
     * 查询消息通知模板
     */
    @Override
    public DrainMsgTemplateVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消息通知模板列表
     */
    @Override
    public PageDataInfo<DrainMsgTemplateVo> queryPageList(DrainMsgTemplateReq request) {
        LambdaQueryWrapper<DrainMsgTemplate> lqw = buildQueryWrapper(request);
        Page<DrainMsgTemplateVo> result = baseMapper.selectVoPage(request.getPageQuery().build(), lqw);
        return PageDataInfo.build(result);
    }

    /**
     * 查询消息通知模板列表
     */
    @Override
    public List<DrainMsgTemplateVo> queryList(DrainMsgTemplateReq request) {
        LambdaQueryWrapper<DrainMsgTemplate> lqw = buildQueryWrapper(request);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrainMsgTemplate> buildQueryWrapper(DrainMsgTemplateReq request) {
        Map<String, Object> params = request.getParams();
        LambdaQueryWrapper<DrainMsgTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getMsgType()), DrainMsgTemplate::getMsgType,request.getMsgType());
        lqw.like(StringUtils.isNotBlank(request.getTemplateTitle()), DrainMsgTemplate::getTemplateTitle,request.getTemplateTitle());
        return lqw;
    }

    /**
     * 新增消息通知模板
     */
    @Override
    public Boolean insertByBo(DrainMsgTemplateBo bo) {
        DrainMsgTemplate add = BeanUtil.toBean(bo, DrainMsgTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消息通知模板
     */
    @Override
    public Boolean updateByBo(DrainMsgTemplateBo bo) {
        DrainMsgTemplate update = BeanUtil.toBean(bo, DrainMsgTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrainMsgTemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除消息通知模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据id删除消息通知模板信息
     */
    @Override
    public Boolean deleteById(String id, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 查询消息模板变量
     * @return
     */
    @Override
    public List<SysDictData> selectTemplateDict()
    {
        List<SysDictData> dictDataList = iSysDictTypeService.selectDictDataByType("msg_template_val");
        return dictDataList;
    }

    /**
     * 根据消息类型查询消息模板
     * @param msgType
     * @return
     */
    @Override
    public DrainMsgTemplateVo  queryMsgTemplate(String msgType)
    {
        DrainMsgTemplateReq request  = new DrainMsgTemplateReq();
        request.setMsgType(msgType);
        List<DrainMsgTemplateVo>  voList  =  queryList(request);
        if(voList!=null && !voList.isEmpty())
        {
            return voList.get(0);
        }

        return null;
    }

}
